<?php
error_reporting(0);
$servername = "localhost:3336";
$username = "root";
$password = "*HM2Ll!#7VCg1K_19F#3@tJ";
$database = "TD_OA";
// 创建连接
$conn = new mysqli($servername, $username, $password, $database);
 
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
function find_id($string,$id)
{
	$string=ltrim($string, ",");
	if($id==""||$id==",")
		return false;
	if(substr($string,-1)!=",")
		$string.=",";
	if(strpos($string,",".$id.",")>0)
		return true;
	if(strpos($string,$id.",")===0)
		return true;
	if(!strstr($id,",") && $string==$id)
		return true;
	return false;
}
//连接成功
$FLOW_ID =$_GET['FLOW_ID'];
$sql = "SELECT MAX(PRCS_ID) as prcs_max from FLOW_PROCESS WHERE FLOW_ID='$FLOW_ID'";
$cursor = $conn->query($sql);
$prcs_max = NULL;
while ($row = mysqli_fetch_assoc($cursor)) {
	$prcs_max = $row["prcs_max"];
}

$query = "SELECT * from FLOW_PROCESS WHERE FLOW_ID='$FLOW_ID' ORDER BY PRCS_ID";
$cursor = $conn->query($query);
$TOP_AUTO = 20;
$LEFT_AUTO = 20;
$PRCS_COUNT = 0;
while ($row = mysqli_fetch_assoc($cursor)) {
	$PRCS_COUNT++;
	$ID = $row["ID"];
	$PRCS_ID = $row["PRCS_ID"];
	// $PRCS_NAME = cstr_replace("\\", "\\\\", $row["PRCS_NAME"]);
	$SYNC_DEAL = $row["SYNC_DEAL"];
	$CHILD_FLOW = $row["CHILD_FLOW"];
	$PRCS_TYPE = $row["PRCS_TYPE"];
	// $PRCS_NAME = htmlspecialchars($PRCS_NAME, ENT_QUOTES, "");
	$PRCS_NAME = iconv('GB2312','UTF-8',$row['PRCS_NAME']);
	$PRCS_IN = iconv('GB2312','UTF-8',$row["PRCS_IN"]);
	$PRCS_OUT = iconv('GB2312','UTF-8',$row["PRCS_OUT"]);
	$PRCS_IN_SET = iconv('GB2312','UTF-8',$row["PRCS_IN_SET"]);
	$PRCS_OUT_SET = iconv('GB2312','UTF-8',$row["PRCS_OUT_SET"]);
	$PRCS_IN = str_replace("'include'", _("'包含'"), $PRCS_IN);
	$PRCS_IN = str_replace("'exclude'", _("'不包含'"), $PRCS_IN);
	$PRCS_OUT = str_replace("'include'", _("'包含'"), $PRCS_OUT);
	$PRCS_OUT = str_replace("'exclude'", _("'不包含'"), $PRCS_OUT);
	$PRCS_CONDITION = "";
	
	if ($PRCS_IN != "") {
		$PRCS_CONDITION .= "<br>" . _("·转入条件列表：") . "\n" . trim($PRCS_IN);
	}

	if ($PRCS_IN_SET != "") {
		$PRCS_CONDITION .= "<br>" . _("·转入条件公式：") . "\n" . trim($PRCS_IN_SET);
	}

	if ($PRCS_OUT != "") {
		$PRCS_CONDITION .= "<br>" . _("·转出条件列表：") . "\n" . $PRCS_OUT;
	}

	if ($PRCS_OUT_SET != "") {
		$PRCS_CONDITION .= "<br>" . _("·转出条件公式：") . "\n" . trim($PRCS_OUT_SET);
	}
	
	$PRCS_CONDITION = str_replace("''", _("'空'"), $PRCS_CONDITION);
	$PRCS_CONDITION = str_replace("'", " ", $PRCS_CONDITION);
	$PRCS_TO = $row["PRCS_TO"];
	if (($PRCS_TO == "") && ($CHILD_FLOW == 0)) {
		if ($PRCS_ID != $prcs_max) {
			$PRCS_TO = $PRCS_ID + 1;
		}
		else {
			$PRCS_TO = "0";
		}
	}

	$PRCS_TO = str_replace(",,", ",", $PRCS_TO);

	if (substr($PRCS_TO, 0, 1) == ",") {
		$PRCS_TO = substr($PRCS_TO, 1);
	}

	$SET_LEFT = $row["SET_LEFT"];
	$SET_TOP = $row["SET_TOP"];

	if ($SET_LEFT == 0) {
		$SET_LEFT = $LEFT_AUTO;
	}

	if ($SET_TOP == 0) {
		$SET_TOP = $TOP_AUTO;
	}

	if (($PRCS_ID % 2) == 0) {
		$LEFT_AUTO += 180;
		$TOP_AUTO = 20;
	}
	else {
		$TOP_AUTO = 200;
	}

	$PRCS_TO_TITLE = $PRCS_TO;
	$PRCS_TO_TITLE = str_replace(",0", "," . _("结束"), $PRCS_TO_TITLE);
	
	if (($PRCS_TO_TITLE != "") && (substr($PRCS_TO_TITLE, 0, 1) == "0")) {
		$PRCS_TO_TITLE = _("结束") . substr($PRCS_TO_TITLE, 1);
	}

	$PRCS_TO_TITLE = _("·下一步骤：") . $PRCS_TO_TITLE;
	
	if ($PRCS_TYPE == 1) {
		$PRCS_TYPE = "child";
	}
	else if ($PRCS_TYPE == 2) {
		$PRCS_TYPE = "out";
	}
	else if ($PRCS_ID == 1) {
		$PRCS_TYPE = "start";
	}
	else {
		if (find_id($PRCS_TO, "0") || ($PRCS_TO == $prcs_max + 1)) {
			$PRCS_TYPE = "end";
		}
		else {
			$PRCS_TYPE = "general";
		}
	}
	
	$PRCS_CONTENT = "<b>" . $PRCS_ID . "</b><br>" . $PRCS_NAME;

	if ($CHILD_FLOW != 0) {
		$PRCS_CONTENT .= _("(子流程)");
	}
	
	$title = $PRCS_TO_TITLE . $PRCS_CONDITION.'<hr>';
	$prcs_array["list"][] = array("id" => $ID, "prcs_id" => $PRCS_ID, "title" => $title, "prcs_content" => $PRCS_CONTENT, "left" => $SET_LEFT, "top" => $SET_TOP, "prcs_type" => $PRCS_TYPE, "to" => $PRCS_TO);
}

$prcs_array["total"] = $PRCS_COUNT;
// print_r($prcs_array);

// header("Cache-Control: no-cache, must-revalidate");
// header("Pragma: no-cache");
// header("Content-type: text/x-json; charset="utf8");
echo json_encode($prcs_array);
?>
